//
//  FilterView.swift
//  koolcar
//
//  Created by xiantian on 2023/11/27.
//

import UIKit

typealias FilterViewBlock = (NSInteger)->Void
class FilterView: UIView {
    var selectBlock: FilterViewBlock?
    private var collectionView: UICollectionView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize.init(width: (kScreenWidth - 40)/4, height: 32)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 4
        self.collectionView = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 32), collectionViewLayout: layout)
        self.collectionView.register(FilterViewItemCell.self, forCellWithReuseIdentifier: "FilterViewItemCell")
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = UIColor.white
        collectionView.contentInset = UIEdgeInsets.init(top: 0, left: 14, bottom: 0, right: 14)
        self.addSubview(self.collectionView)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


extension FilterView: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FilterViewItemCell", for: indexPath) as! FilterViewItemCell
        switch indexPath.row {
            case 0:
                cell.titleLab.text = MMLocalizedString("brands", comment: "品牌")
            case 1:
                cell.titleLab.text = MMLocalizedString("price_range", comment: "价格")
            case 2:
                cell.titleLab.text = MMLocalizedString("vehicle_model", comment: "车型")
            case 3:
                cell.titleLab.text = MMLocalizedString("more", comment: "更多")
            default:
                ()
        }
        
        cell.titleLab.sizeToFit()
        cell.updateConstraints()
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectBlock?(indexPath.row)
    }
}
 

class FilterViewItemCell: UICollectionViewCell {
    var titleLab: UILabel!
    var arrowImgView: UIImageView = {
        let tempImg = UIImageView()
        tempImg.image = UIImage.init(named: "icon_filter_down")
        tempImg.contentMode = .scaleAspectFit
        return tempImg
    }()
    var bkgView: UIView = {
        let tempView = UIView()
        tempView.backgroundColor = Theme.viewBkgGrayColor
        tempView.layer.cornerRadius = 16
        tempView.layer.masksToBounds = true
        return tempView
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = Theme.whiteColor
        contentView.addSubview(self.bkgView)
        titleLab = UILabel()
        titleLab.textColor = Theme.titleLabColor
        titleLab.backgroundColor = UIColor.clear
        titleLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        titleLab.numberOfLines = 1
        titleLab.lineBreakMode = .byTruncatingTail
        titleLab.textAlignment = .center
        contentView.addSubview(titleLab)
        contentView.addSubview(arrowImgView)
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        bkgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        titleLab.snp.makeConstraints { make in
            make.centerX.equalToSuperview().offset(-6)
            make.centerY.equalTo(16)
        }
        
        arrowImgView.snp.makeConstraints { make in
            make.left.equalTo(titleLab.snp.right).offset(4)
            make.centerY.equalTo(titleLab.snp.centerY)
            make.width.equalTo(10)
            make.height.equalTo(6)
        }
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
